/*!
 * Page: pages/news/systemMsg/SystemMsg
 * License: MIT
 * Created: 2021-04-08 11:07
 */
import { $event } from '/utils'
import { userApi } from '../../../api/user'
import { checkAuth } from '/store/actions'
import { GlobalData } from '/store/state'
/* eslint-disable @typescript-eslint/no-object-literal-type-assertion */

// 处理认证操作
function handleUserVerifyNotice(itemData: any) {
  const user = wx.getStorageSync(`${GlobalData.server}/logged`).data || {}
  if (!user?.verification) {
    wx.showToast({
      title: '认证信息已被删除',
      icon: 'none',
    })
  }
  const type = user?.verification?.type

  switch (itemData.data.state) {
    // 认证通过
    case 'passed':
      switch (type) {
        case 'user':
          wx.navigateTo({
            url: '/mine/user-verified/user-verified',
          })
          break
        case 'org':
          wx.navigateTo({
            url: '/mine/org-verified/org-verified',
          })
          break
      }
      break
    // 被拒绝
    case 'rejected':
      switch (type) {
        case 'user':
          wx.navigateTo({
            url: '/mine/add-user-verification/add-user-verification?load=1',
          })
          break
        case 'org':
          wx.navigateTo({
            url: '/mine/add-org-verification/add-org-verification?load=1',
          })
          break
      }

      break
    // 被重置
    case 'recovery':
      switch (type) {
        case 'user':
          wx.navigateTo({
            url: '/mine/user-verified/user-verified',
          })
          break
        case 'org':
          wx.navigateTo({
            url: '/mine/org-verified/org-verified',
          })
          break
      }
      break
    // 被删除
    case 'destroy':
      wx.showToast({
        title: '认证信息已被删除',
        icon: 'none',
      })
      break

    default:
      break
  }
}

// 处理举报操作
function handleReportNotice(itemData: any) {
  switch (itemData.data.resource.type) {
    // 举报的用户
    case 'users':
      wx.navigateTo({ url: `/mine/homePage/homePage?id=${itemData.data.resource.id}` })
      break
    // 举报的动态
    case 'feeds':
      wx.navigateTo({ url: `/pages/feed/detail/detail?feedId=${itemData.data.resource.id}` })
      break
    // 举报的评论
    case 'comments':
      break
    // 举报的圈子
    case 'groups':
      wx.navigateTo({ url: `/pages/topic/detail/detail?topicId=${itemData.data.resource.id}` })
      break
    // 举报的话题
    case 'themes':
      wx.navigateTo({ url: `/pages/theme/detail/detail?id=${itemData.data.resource.id}` })
      break
    // 举报的问题
    case 'wendas':
      wx.navigateTo({
        url: `/index/knowAboutDetail/knowAboutDetail?id=${itemData.data.resource.id}`,
      })
      break
    // 举报的回答
    case 'question-answers':
      wx.navigateTo({ url: `/pages/feed/detail/detail?feedId=${itemData.data.resource.id}` })
      break
    // 举报的活动
    case 'events':
      wx.navigateTo({ url: `/pages/activity/detail/detail?id=${itemData.data.resource.id}` })
      break

    // 举报的知识
    case 'knowledge':
      wx.navigateTo({ url: `/index/knowledge/detail/detail?id=${itemData.data.resource.id}` })
      break

    // 举报的知识章节
    case 'knowledge_chapters':
      wx.navigateTo({
        url: `/index/knowledge/learnKnowledge/learnKnowledge?id=${itemData.data.resource.id}`,
      })
      break

    // 举报的商品
    case 'mall_commodities':
      wx.navigateTo({
        url: `/mall/detail/detail?commodityId=${itemData.data.resource.id}`,
      })
      break
    // 举报的资讯
    case 'infos':
      wx.navigateTo({
        url: `/index/information/detail/detail?id=${itemData.data.resource.id}`,
      })
      break

    default:
      break
  }
}

Page({
  data: {
    list: [] as any,
    type: 'system',
    number: 0,
    page: 1,
    nomore: false,
    isLastPage: false,
  },
  async onLoad(query: any) {
    //消除小红点
    if (query.number > 0) {
      userApi.removeUnreadTips(this.data.type).then(() => {
        $event.emit('after:notification')
      })
    }
    this.getList()
  },
  async onPullDownRefresh() {
    this.setData({
      page: 1,
      nomore: false,
    })
    await this.getList()
    wx.stopPullDownRefresh()
  },
  onReachBottom() {
    if (this.data.isLastPage) return
    this.setData({
      page: this.data.page + 1,
    })
    this.getList(true)
  },

  async getList(isLoadMore = false) {
    if (this.data.nomore) return
    const list = await userApi.getUserNewsDetail({ type: this.data.type, page: this.data.page })
    this.setData({
      list: isLoadMore ? [...this.data.list, ...list.data] : list.data,
      nomore: list.length < 15,
      page: list.meta.current_page,
      isLastPage: list.meta.current_page === list.meta.last_page,
    })
  },
  async goDetail(e: WechatMiniprogram.CustomEvent) {
    await checkAuth()
    const itemData = e.currentTarget.dataset.data

    switch (itemData.data.type) {
      // 认证
      case 'verifications':
        handleUserVerifyNotice(itemData)
        break
      // 加入圈子
      case 'join-group':
        wx.navigateTo({ url: `/pages/topic/detail/detail?topicId=${itemData.data.resource.id}` })
        break
      // 回答采纳
      case 'wenda-adoption':
        wx.navigateTo({ url: `/pages/feed/detail/detail?feedId=${itemData.data.resource.id}` })
        break

      // 动态被推荐到分类
      case 'feed-push':
        wx.navigateTo({ url: `/pages/feed/detail/detail?feedId=${itemData.data.resource.id}` })
        break

      // 创建圈子
      case 'create-group':
        wx.navigateTo({ url: `/pages/topic/detail/detail?topicId=${itemData.data.resource.id}` })
        break
      // 举报
      case 'report':
        handleReportNotice(itemData)
        break
      // 提现
      case 'user-cash':
        wx.navigateTo({
          url: `/mine/withdrawDetail/withdrawDetail?orderId=${itemData.data.resource.id}`,
        })
        break
      // 订单
      case 'mall-order':
        // //商家(类似销售订进入) seller 买家 buyer
        // eslint-disable-next-line no-case-declarations
        const isSeller = 'seller' === itemData.data.resource.type
        wx.navigateTo({
          url: `/mall/orderDetail/orderDetail?id=${isSeller ? 4 : 5}&orderId=${itemData.data.resource.id
            }`,
        })
        break
      default:
        break
    }
  },
})
